#include "Generator.h"


aal_sieckolejowa::Generator::Generator(int min_branches, int max_branches, int num_verticles, const char* file_name){

	/* initialize random seed: */
	if(num_verticles==1)
		srand ( time(NULL) );



	int difference = max_branches - min_branches;
	int numberBranches;

	UndirectedGraph railwayTree(num_verticles); //od zera do num_verticles-1


	int verticlesLeft = num_verticles;
	int first=0;
	int last=0;
	int firstAvailable=1;//numer pierwszego, niepolaczonego wierzcholka

	--verticlesLeft; // bo na poczatku jest wierzcholek, od ktorego zaczynamy
	//petla wypelniajaca cale drzewo
	while(verticlesLeft>0){

		//petla wypelniajaca jeden pozioma

		for(int i = first; i<=last;++i){

			if(difference!=0)numberBranches = rand() % difference + min_branches;
			else
				numberBranches=min_branches;

			if (numberBranches > verticlesLeft ) numberBranches = verticlesLeft;

			for(int j=firstAvailable ;j<firstAvailable+numberBranches; ++j){
				//wstawiaj do drzewa numerBranches elementow polaczonych z i tym wierzcholkiem. Kazdy element o indeksie j.

				add_edge(i, j, railwayTree );
			}
			verticlesLeft-= numberBranches;
			if(verticlesLeft==0) {break;}

			firstAvailable+=numberBranches;

		}
		first=last+1;
		last=firstAvailable-1;
	}


	std::ofstream myfile(file_name);

	//// Zapisuje drzewo do pliku w formie nie do odczytania przez normalnego czlowieka.:)

	//archive::text_oarchive oa(myfile);
	//serialization::save(oa,railwayTree,1678905)  ;
	////oa << railwayTree;

	/*const int V = 3;
	UndirectedGraph undigraph(V);*/

	myfile<<num_verticles<<std::endl;
	graph_traits < UndirectedGraph >::out_edge_iterator out, out_end;
	for(int vertex_index =0;vertex_index<num_verticles;++vertex_index){
		myfile << vertex_index << " ";
		for (tie(out, out_end) = out_edges(vertex(vertex_index, railwayTree), railwayTree); out != out_end; ++out){
			int t =  target(*out,railwayTree);
			if (t>vertex_index)
				myfile << t << " ";
		}

		myfile <<std::endl;
	}

	return;
}



aal_sieckolejowa::Generator::Generator()
{
	Generator(0,2,5,"zupa");
}

aal_sieckolejowa::Generator::~Generator(void)
{
}



void aal_sieckolejowa::Generator::save(const char*){



}